package com.edu.exam.entity.examination.control;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 答题进度实体类
 * 所属模块：E-01 考试执行模块
 * 对应数据库表：answer_progress
 *
 * <p>答题进度实体用于记录和管理学生答题的进度信息，包括题目完成情况、
 * 时间统计、进度分析等。支持详细的进度跟踪和统计分析。</p>
 *
 * <p>核心功能：
 * - 进度跟踪：实时跟踪答题进度和完成情况
 * - 时间统计：统计答题用时和平均时间
 * - 题目状态：管理每个题目的答题状态
 * - 进度分析：提供进度分析和预测
 * - 统计报告：生成详细的进度统计报告</p>
 *
 * @author System
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
public class AnswerProgress {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 进度ID
     *
     * <p>进度记录的唯一标识符，采用UUID格式生成。</p>
     */
    @Column(name = "progress_id", nullable = false, length = 32, unique = true)
    private String progressId;

    /**
     * 会话ID
     *
     * <p>关联的答题控制会话ID。</p>
     */
    @Column(name = "session_id", nullable = false, length = 32)
    private String sessionId;

    /**
     * 学生ID
     *
     * <p>答题学生的ID。</p>
     */
    @Column(name = "student_id", nullable = false, length = 32)
    private String studentId;

    /**
     * 总题目数
     *
     * <p>试卷的总题目数。</p>
     */
    @Column(name = "total_questions", nullable = false)
    private Integer totalQuestions = 0;

    /**
     * 已答题数
     *
     * <p>已答题目数量。</p>
     */
    @Column(name = "answered_questions", nullable = false)
    private Integer answeredQuestions = 0;

    /**
     * 已复习题数
     *
     * <p>已复习题目数量。</p>
     */
    @Column(name = "reviewed_questions", nullable = false)
    private Integer reviewedQuestions = 0;

    /**
     * 标记题数
     *
     * <p>标记题目数量。</p>
     */
    @Column(name = "marked_questions", nullable = false)
    private Integer markedQuestions = 0;

    /**
     * 异常题数
     *
     * <p>异常题目数量。</p>
     */
    @Column(name = "flagged_questions", nullable = false)
    private Integer flaggedQuestions = 0;

    /**
     * 完成百分比
     *
     * <p>答题完成百分比。</p>
     */
    @Column(name = "completion_percentage", nullable = false, precision = 5, scale = 2)
    private BigDecimal completionPercentage = BigDecimal.ZERO;

    /**
     * 答题百分比
     *
     * <p>已答题百分比。</p>
     */
    @Column(name = "answer_percentage", nullable = false, precision = 5, scale = 2)
    private BigDecimal answerPercentage = BigDecimal.ZERO;

    /**
     * 复习百分比
     *
     * <p>已复习题百分比。</p>
     */
    @Column(name = "review_percentage", nullable = false, precision = 5, scale = 2)
    private BigDecimal reviewPercentage = BigDecimal.ZERO;

    /**
     * 总用时（秒）
     *
     * <p>答题总用时（秒）。</p>
     */
    @Column(name = "total_time_spent", nullable = false)
    private Integer totalTimeSpent = 0;

    /**
     * 平均每题用时（秒）
     *
     * <p>平均每题答题用时（秒）。</p>
     */
    @Column(name = "average_time_per_question", nullable = false)
    private Integer averageTimePerQuestion = 0;

    /**
     * 剩余时间（秒）
     *
     * <p>预估剩余答题时间（秒）。</p>
     */
    @Column(name = "remaining_time", nullable = false)
    private Integer remainingTime = 0;

    /**
     * 最快用时（秒）
     *
     * <p>单题最快用时（秒）。</p>
     */
    @Column(name = "fastest_time")
    private Integer fastestTime;

    /**
     * 最慢用时（秒）
     *
     * <p>单题最慢用时（秒）。</p>
     */
    @Column(name = "slowest_time")
    private Integer slowestTime;

    /**
     * 最后活动时间
     *
     * <p>最后答题活动时间。</p>
     */
    @Column(name = "last_activity_time")
    private LocalDateTime lastActivityTime;

    /**
     * 题目状态映射
     *
     * <p>JSON格式的题目状态映射。</p>
     */
    @Column(name = "question_status_map", columnDefinition = "TEXT")
    private String questionStatusMap;

    /**
     * 进度快照
     *
     * <p>JSON格式的进度快照数据。</p>
     */
    @Column(name = "progress_snapshot", columnDefinition = "TEXT")
    private String progressSnapshot;

    /**
     * 进度分析
     *
     * <p>JSON格式的进度分析结果。</p>
     */
    @Column(name = "progress_analysis", columnDefinition = "TEXT")
    private String progressAnalysis;

    /**
     * 时间分布
     *
     * <p>JSON格式的时间分布数据。</p>
     */
    @Column(name = "time_distribution", columnDefinition = "TEXT")
    private String timeDistribution;

    /**
     * 难度分布
     *
     * <p>JSON格式的难度分布数据。</p>
     */
    @Column(name = "difficulty_distribution", columnDefinition = "TEXT")
    private String difficultyDistribution;

    /**
     * 进度预警
     *
     * <p>JSON格式的进度预警信息。</p>
     */
    @Column(name = "progress_alerts", columnDefinition = "TEXT")
    private String progressAlerts;

    /**
     * 进度建议
     *
     * <p>JSON格式的进度建议。</p>
     */
    @Column(name = "progress_recommendations", columnDefinition = "TEXT")
    private String progressRecommendations;

    /**
     * 进度标签
     *
     * <p>JSON格式的进度标签。</p>
     */
    @Column(name = "progress_tags", columnDefinition = "TEXT")
    private String progressTags;

    /**
     * 进度备注
     *
     * <p>进度的备注信息。</p>
     */
    @Column(name = "progress_notes", columnDefinition = "TEXT")
    private String progressNotes;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 版本号
     */
    @Column(name = "version", nullable = false)
    private Integer version = 1;

    /**
     * 是否删除
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;

    // ===== 业务方法 =====
}